import { createStore } from 'vuex';

const ORIGINAL_MENUS = 'ORIGINAL_MENUS';

export default createStore({
  state: {
    original_menus: JSON.parse(<string>window.sessionStorage.getItem(ORIGINAL_MENUS)) || [],
    menuTree: [],
    moduleMenuTree: []
  },
  mutations: {
    INIT_USER_DATA (state, payload) {
      state.original_menus = payload.menuData;
      state.menuTree = payload.menuDataTree;
      // 只需要保存原始的menuData数据即可
      window.sessionStorage.setItem(ORIGINAL_MENUS, JSON.stringify(payload.menuData));
      console.log('store >>> INIT_USER_DATA 收到的 payload = ', payload);
    },
    INIT_MODULE_MENU (state, data) {
      // 用于点击一级模块菜单的时候，锁定二级菜单的菜单树
      state.moduleMenuTree = data;
    }
  },
  actions: {
    INIT_USER_DATA_ASYNC (context, data) {
      context.commit('INIT_USER_DATA', data);
    }
  },
  modules: {
  }
});
